<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class MrmidiController extends BaseController {

    //迷笛先生列表
	public function index() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['user_name'])) {
            $data['user_name'] = array('like',"%".trim($_GET['user_name'])."%");
        }
        if (!empty($_GET['user_phone'])) {
            $data['user_phone'] = array('like',"%".trim($_GET['user_phone'])."%");
        }
        unset($data['p']);
        $data['user_display'] = 1;
        //默认显示审核成功的迷笛先生
        $data['mrmidi_auth_status'] = isset($_GET['mrmidi_auth_status'])?$_GET['mrmidi_auth_status']:2;

        $count = M('mrmidi')->alias('m')->field('mrmidi_user_id')->join('left join midi_user as u on m.mrmidi_user_id = u.user_id left join midi_city as c on u.user_city = c.city_id')->where($data)->group('user_id')->select();
        $c = count($count);
        $page = getpage($c,10);
        $mrmidi_arr = M('mrmidi')->field('user_id,user_name,user_phone,city_name,mrmidi_major_id,mrmidi_level,mrmidi_auth_status')->alias('m')->join('left join midi_user as u on m.mrmidi_user_id = u.user_id left join midi_city as c on u.user_city = c.city_id')->where($data)->group('u.user_id')->order('mrmidi_id desc')->limit($page->firstRow, $page->listRows)->select();
        $city_arr = M('city')->select();
        $this->assign('city_arr', $city_arr);
        $this->assign('mrmidi_arr', $mrmidi_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //删除迷笛先生
    public function mrmidi_del(){
        $data['user_id'] = I('get.mrmidi_id');
        $data['user_display'] = 0;
        if(M('user')->save($data)){
            $this->redirect('index','删除迷笛先生成功');
        }else{
            $this->error('删除迷笛先生失败');
        }
    }

    //迷笛先生详情
    public function mrmidi_details(){
        $data['user_id'] = I('get.mrmidi_id');
        $user_arr = M('user')->alias('u')->join('left join midi_city as c on u.user_city = c.city_id')->join('left join midi_midiclub as m on m.midiclub_id = u.user_midiclub_id')->where($data)->find();
        $condition['mrmidi_user_id'] = $data['user_id'];
        $mrmidi_arr = M('mrmidi')->alias('m')->join('right join midi_certificate as c on m.mrmidi_certificate_no = c.certificate_num')->where('mrmidi_user_id = '.$data['user_id'])->select();
        $this->assign('mrmidi_arr',$mrmidi_arr);
        $this->assign('user_arr',$user_arr);
        $this->display();
    }

    //添加迷笛先生
    // public function mrmidi_add(){
    //     if (I('post.action') == 'mrmidi_add_action') {
    //         unset($_POST['action']);
    //         $this->mrmidi_add_action();
    //         exit();
    //     }
    //     $this->display();
    // }

    //添加迷笛先生操作
    // public function mrmidi_add_action(){
    //     if($_POST != array_filter($_POST)){
    //         $this->error('选项不能有空值');
    //     }
    //     $data['user_id'] = $_POST['mrmidi_user_id'];
    //     $user = M('user')->field('user_id,user_role')->where($data)->find();
    //     if(empty($user)){
    //         $this->error('用户不存在');
    //     }
    //     $where['mrmidi_user_id'] = $_POST['mrmidi_user_id'];
    //     $where['mrmidi_major_id'] = $_POST['mrmidi_major_id'];
    //     if($mrmidi_arr = M('mrmidi')->where($where)->find()){
    //         if($mrmidi_arr['mrmidi_auth_status'] == 2){
    //             $this->error('该用户已经是这个专业的迷笛先生，不能重复添加！');
    //         }else{
    //             $mrmidi['mrmidi_auth_status'] = 2;
    //             $mrmidi['mrmidi_id'] = $mrmidi_arr['mrmidi_id'];
    //             if(M('mrmidi')->save($mrmidi)){
    //                 if ($user['user_role'] == 1) {//以前是考生，角色改为2
    //                     $data['user_role'] = 2;
    //                     M('user')->save($data);
    //                 }elseif ($user['user_role'] == 4) {//以前是考官，角色改为3
    //                     $data['user_role'] = 3;
    //                     M('user')->save($data);
    //                 }
    //                 $this->redirect('index','添加迷笛先生成功');
    //             }else{
    //                 $this->error('添加迷笛先生失败');
    //             }
    //         }
    //     }
    //     $_POST['mrmidi_auth_status'] = 2;
    //     if(M('mrmidi')->add($_POST)){
    //         if ($user['user_role'] == 1) {//以前是考生，角色改为2
    //             $data['user_role'] = 2;
    //             M('user')->save($data);
    //         }elseif ($user['user_role'] == 4) {//以前是考官，角色改为3
    //             $data['user_role'] = 3;
    //             M('user')->save($data);
    //         }
    //         $this->redirect('index','添加迷笛先生成功');
    //     }else{
    //         $this->error('添加迷笛先生失败');
    //     }

    // }

    //修改迷笛先生信息
    public function mrmidi_edit(){
        if (I('post.action') == 'mrmidi_edit_action') {
            $this->mrmidi_edit_action();
            exit();
        }
        $data['user_id'] = I('get.mrmidi_id');
        $user_arr = M('user')->alias('u')->join('left join midi_city as c on u.user_city = c.city_id')->where($data)->find();
        $mrmidi_arr = M('mrmidi')->where('mrmidi_user_id = '.$data['user_id'])->select();
        $where['certificate_type'] = 2;
        $where['certificate_user_id'] = $data['user_id'];
        $certificate = M('certificate')->where($where)->order('certificate_level desc')->find();
        $this->assign('certificate',$certificate);
        $this->assign('mrmidi_arr',$mrmidi_arr);
        $this->assign('user_arr',$user_arr);
        $sql = "select * from midi_city order by CONVERT( city_name USING gbk ) COLLATE gbk_chinese_ci ASC";
        $citys = M()->query($sql);
        $this->assign('citys',$citys);
        $club['midiclub_display'] = 1;
        $club['midiclub_type'] = array('IN','2,3');
        $clubs = M('midiclub')->field('midiclub_id,midiclub_name')->where($club)->select();
        $this->assign('clubs',$clubs);
        $this->display();
    }

    //修改迷笛先生信息操作
    public function mrmidi_edit_action(){
        $actionedit = isset($_POST['actionedit'])?$_POST['actionedit']:'';
        if($actionedit == 'edit_mrmidi'){
            unset($_POST['action']);
            unset($_POST['actionedit']);
            $data['mrmidi_user_id'] = $_POST['mrmidi_user_id'];
            $data['mrmidi_major_id'] = $_POST['mrmidi_major_id'];
            $mrmidi = M('mrmidi')->where($data)->find();
            $_POST['mrmidi_id'] = $mrmidi['mrmidi_id'];
            if(M('mrmidi')->save($_POST)){
                if ($_POST['mrmidi_auth_status'] == 2) {
                    $user['user_id'] = $_POST['mrmidi_user_id'];
                    $user['user_role'] = 2;
                    M('user')->save($user);
                }
                $this->success('修改信息成功');
            }else{
                $this->error('没有信息被修改');
            }
            exit();
        }
        $finfo = upload($_FILES['avatar']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['user_avatar'] = $finfo['filepath'];
        }
        if(M('user')->save($_POST)){
            $this->success('修改信息成功');
        }else{
            $this->error('没有信息被修改');
        }
    }

    /**
    * 数组转xls格式的excel文件
    * @param  array  $data      需要生成excel文件的数组
    * @param  string $filename  生成的excel文件名
    *      示例数据：
    *       $data = array(
    *          array(NULL, 2010, 2011, 2012),
    *           array('Q1',   12,   15,   21),
    *           array('Q2',   56,   73,   86),
    *           array('Q3',   52,   61,   69),
    *           array('Q4',   30,   32,    0),
    *          );
    */
    public function mrmidi_export(){
        //excel表格导出审核中的迷笛先生
        $condition['user_display'] = 1;
        $condition['mrmidi_auth_status'] = 1;//审核中

        $mrmidi_arr = M('mrmidi')->field('user_id,user_name,user_sex,user_phone,user_weixin,mrmidi_major_id,mrmidi_level,mrmidi_link,user_orgname,user_midigraduate,user_introduction,mrmidi_applytime')->alias('m')->join('midi_user as u on m.mrmidi_user_id = u.user_id')->where($condition)->select();
        $data = array();
        $title = array('序号','迷笛ID','姓名','性别','电话','微信','专业','等级','作品链接','所属机构','迷笛毕业生','个人简介','申请时间');
        array_push($data, $title);
        if (empty($mrmidi_arr)) {
            $this->error('没有审核中的迷笛先生');
            exit();
        }
        foreach ($mrmidi_arr as $k => $v) {
            //迷笛ID
            $user_id = $v['user_id'];
            //姓名
            $user_name = $v['user_name'];
            //性别
            if ($v['user_sex'] == 1) {
                $user_sex = '男';
            }elseif ($v['user_sex'] == 2) {
                $user_sex = '女';
            }
            //电话
            $user_phone = $v['user_phone'];
            //微信
            $user_weixin = $v['user_weixin'];
            //专业
            if ($v['mrmidi_major_id'] == 1) {
                $mrmidi_major_id = '电吉他';
            }elseif ($v['mrmidi_major_id'] == 2) {
                $mrmidi_major_id = '爵士鼓';
            }elseif ($v['mrmidi_major_id'] == 3) {
                $mrmidi_major_id = '电贝司';
            }
            //等级
            if ($v['mrmidi_level'] == 1) {
                $mrmidi_level = '初级';
            }elseif ($v['mrmidi_level'] == 2) {
                $mrmidi_level = '中级';
            }elseif ($v['mrmidi_level'] == 3) {
                $mrmidi_level = '高级';
            }elseif ($v['mrmidi_level'] == 4) {
                $mrmidi_level = '特级';
            }
            //作品链接
            $mrmidi_link = $v['mrmidi_link'];
            //所属机构
            $user_orgname = $v['user_orgname'];
            //迷笛毕业生
            if ($v['user_midigraduate'] == 1) {
                $user_midigraduate = '是';
            }elseif ($v['user_midigraduate'] == 2) {
                $user_midigraduate = '否';
            }
            //个人介绍
            $user_introduction = $v['user_introduction'];
            //申请时间
            $mrmidi_applytime = date('Y-m-d H:i',$v['mrmidi_applytime']);
            $arr = array();
            $arr = array(($k+1),$user_id,$user_name,$user_sex,$user_phone,$user_weixin,$mrmidi_major_id,$mrmidi_level,$mrmidi_link,$user_orgname,$user_midigraduate,$user_introduction,$mrmidi_applytime);
            array_push($data, $arr);
        }
        $filename = date('YmdHis',time()).rand(1000,9999).'.xls';
        create_xls($data,$filename);
    }

}